package com.yinhai.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.yinhai.model.ContactsUser;
import com.yinhai.view.CustomGridView;

/**
 * 查看群组成员
 * 
 * @author lixh
 *
 */
public class ChatViewGroupActivity extends Activity {
	private CustomGridView gridView;
	private List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
	private SimpleAdapter adapter;
	private boolean itemDeleteFlag = false;
	private List<String> phoneList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat_view_group);

		TextView textView = (TextView) findViewById(R.id.tvtest);

		gridView = (CustomGridView) findViewById(R.id.gridview);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "");
		map.put("img", R.drawable.title_03);
		map.put("deleteFlag", false);
		datas.add(map);
		adapter = new SimpleAdapter(this, datas, R.layout.gridview_group_item,
				new String[] { "img", "name" }, new int[] { R.id.img,
						R.id.title }) {
			public View getView(int position, View convertView, ViewGroup parent) {
				View view = super.getView(position, convertView, parent);
				if (Boolean.valueOf(datas.get(position).get("deleteFlag")
						.toString())) {
					view.findViewById(R.id.btn_delete).setVisibility(
							View.VISIBLE);
				} else {
					view.findViewById(R.id.btn_delete).setVisibility(View.GONE);
				}

				return view;
			}
		};
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (itemDeleteFlag
						&& Boolean.valueOf(datas.get(arg2).get("deleteFlag")
								.toString())) {
					phoneList.remove(((ContactsUser)datas.get(arg2).get("data")).getTelPhone());
					datas.remove(arg2);
					adapter.notifyDataSetChanged();
				} else if ((Integer) datas.get(arg2).get("img") == R.drawable.title_03) {
					itemDeleteFlag = false;
					for (Map<String, Object> map : datas) {
						map.put("deleteFlag", itemDeleteFlag);
					}
					adapter.notifyDataSetChanged();
					Intent intent = new Intent(ChatViewGroupActivity.this,
							ContactActivity.class);
					intent.putExtra("flag", "chatGroup");
					startActivityForResult(intent, 123456);
				}

			}
		});
		gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				itemDeleteFlag = true;
				for (Map<String, Object> map : datas) {
					if (!((Integer) map.get("img") == R.drawable.title_03))
						map.put("deleteFlag", itemDeleteFlag);
				}
				adapter.notifyDataSetChanged();
				return true;
			}
		});
		gridView.setOnTouchBlankPositionListener(new CustomGridView.OnTouchBlankPositionListener() {

			@Override
			public void onTouchBlank(MotionEvent event) {
				itemDeleteFlag = false;
				for (Map<String, Object> map : datas) {
					map.put("deleteFlag", itemDeleteFlag);
				}
				adapter.notifyDataSetChanged();
			}
		});

	}

	// 返回
	public void doBack(View view) {
		onBackPressed();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 123456 && resultCode == RESULT_OK) {
			for (ContactsUser user : (List<ContactsUser>) data
					.getSerializableExtra("data")) {
				if (phoneList.contains(user.getTelPhone())) {
					continue;
				}
				phoneList.add(user.getTelPhone());
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", user.getName());
				map.put("data", user);
				map.put("deleteFlag", false);
				map.put("img", R.drawable.dial_head);
				datas.add(datas.size() - 1, map);

			}
			adapter.notifyDataSetChanged();
		}
	}
}
